﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Net;
using Newtonsoft.Json;
using Lmm;
namespace DemoReception
{
    public class CHotelConnect
    {
        private static string iRequestIp = "127.0.0.1";
        private static string iPort = "5203";
        private static string iRequestUrl = "";
        public static bool Init()
        {
            iRequestIp = ConfigurationManager.AppSettings["server_ip"];
            iPort = ConfigurationManager.AppSettings["server_port"];
            iRequestUrl = string.Format("http://{0}:{1}/", iRequestIp, iPort);
            CCommonFunction.InitLogger();
            return true;
        }

        #region setting
        /// <summary>
        /// 获取房间类型
        /// </summary>
        /// <param name="aHotelId"></param>
        /// <param name="aType"></param>
        /// <returns></returns>
        public static int GetHotelRoomType(out List<KeyValuePair<Int64, string>> aType,string aCode)
        {
            int Rst = 0;
            string Url = string.Format("{0}basic/getroomtype?code={1}", iRequestUrl,aCode);
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aType = null;
            try
            {
                CGetRoomTypeResult RespRst = JsonConvert.DeserializeObject<CGetRoomTypeResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aType = new List<KeyValuePair<Int64, string>>();
                    foreach (EmRoomType RoomType in RespRst.iTypes)
                    {
                        aType.Add(new KeyValuePair<Int64, string>(RoomType.iId, RoomType.iTypeName));
                    }
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        /// <summary>
        /// 获取所有房间房态
        /// </summary>
        /// <param name="aHotelId"></param>
        /// <param name="aRooms"></param>
        /// <returns></returns>
        public static int GetHotelRoomByTypes(out List<CHotelRoom> aRooms, string aCode, int aTypeId = 0)
        {
            int Rst = 0;
            string Url = string.Format("{0}basic/gethotelroombytype?typeid={1}&code={2}", iRequestUrl, aTypeId,aCode);
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aRooms = null;
            try
            {
                CGetRoomsResult RespRst = JsonConvert.DeserializeObject<CGetRoomsResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aRooms = new List<CHotelRoom>();
                    foreach (EmRoom Room in RespRst.iRooms)
                    {
                        aRooms.Add(new CHotelRoom(Room.iId, Room.iRoomNo, Room.iRoomType, Room.iHasGuest, Room.iIsDirty, Room.iExcept));
                    }
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int GetHotels(out List<KeyValuePair<string, string>> aHotels)
        {
            int Rst = 0;
            string Url = iRequestUrl + "basic/gethotels";
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aHotels = null;
            try
            {
                CGetHotelsResult RespRst = JsonConvert.DeserializeObject<CGetHotelsResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aHotels = new List<KeyValuePair<string, string>>();
                    foreach (EmHotel Hotel in RespRst.iHotels)
                    {
                        aHotels.Add(new KeyValuePair<string, string>(Hotel.iCode, Hotel.iName));
                    }
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int NewHotel(string aCode, string aName, string aAddr)
        {
            int Rst = 0;
            string Url = iRequestUrl + "basic/sethotel";
            JObject Param = new JObject();
            Param["code"] = aCode;
            Param["name"] = aName;
            Param["addr"] = aAddr;
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNodeJsResult RespRst = JsonConvert.DeserializeObject<CNodeJsResult>(Result);
                Rst = RespRst.iResult;

            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int SetHotelRoomType(out Int64 aNewId, string aCode, Int64 aTypeId,string aTypeName)
        {
            int Rst = 0;
            aNewId = 0;
            string Url = iRequestUrl + "basic/setroomtype";
            JObject Param = new JObject();
            Param["code"] = aCode;
            Param["typeid"] = aTypeId;
            Param["name"] = aTypeName;
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNewItemResult RespRst = JsonConvert.DeserializeObject<CNewItemResult>(Result);
                Rst = RespRst.iResult;
                aNewId = RespRst.iNewId;
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int NewHotelRoom(out Int64 aRoomId, string aCode, Int64 aTypeId, string aRoomNo)
        {
            int Rst = 0;
            string Url = iRequestUrl + "basic/sethotelroom";
            JObject Param = new JObject();
            Param["code"] = aCode;
            Param["typeid"] = aTypeId;
            Param["room"] = aRoomNo;
            aRoomId = 0;
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNewItemResult RespRst = JsonConvert.DeserializeObject<CNewItemResult>(Result);
                Rst = RespRst.iResult;
                aRoomId = RespRst.iNewId;
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }
        #endregion

        #region Room
        public static int SetDailyDirty(string aCode)
        {
            int Rst = 0;
            string Url = iRequestUrl + "room/updatecleanroom";
            JObject Param = new JObject();
            Param["code"] = aCode;
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNodeJsResult RespRst = JsonConvert.DeserializeObject<CNodeJsResult>(Result);
                Rst = RespRst.iResult;

            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int UpdateRoomState(Int64 aRoomId, List<KeyValuePair<string, int>> UpdateKey)
        {
            int Rst = 0;
            string Url = iRequestUrl + "room/updateroomstat";
            JObject Param = new JObject();
            Param["roomid"] = aRoomId;
            JArray KvArray = new JArray();
            foreach (KeyValuePair<string, int> Kpair in UpdateKey)
            {
                JObject Kv = new JObject();
                Kv["k"] = Kpair.Key;
                Kv["v"] = Kpair.Value;
                KvArray.Add(Kv);
            }
            Param["stat"] = KvArray.ToString(Formatting.None);
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNodeJsResult RespRst = JsonConvert.DeserializeObject<CNodeJsResult>(Result);
                Rst = RespRst.iResult;

            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int GetAvailableRoom(out List<CHotelRoom> aRooms, string aCode, Int64 aTypeId, DateTime aDateFr, DateTime aDateTo)
        {
            int Rst = 0;
            string Url = string.Format("{0}room/getavailableroom?code={1}&typeid={2}&datefr={3}&dateto={4} "
                , iRequestUrl
                , aCode
                , aTypeId
                , LmmCommonFunction.DateTimeToUTC(aDateFr)
                , LmmCommonFunction.DateTimeToUTC(aDateTo));
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aRooms = null;
            try
            {
                CGetRoomsResult RespRst = JsonConvert.DeserializeObject<CGetRoomsResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aRooms = new List<CHotelRoom>();
                    foreach (EmRoom Room in RespRst.iRooms)
                    {
                        aRooms.Add(new CHotelRoom(Room.iId, Room.iRoomNo, Room.iRoomType, Room.iHasGuest, Room.iIsDirty, Room.iExcept));
                    }
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        #endregion

        #region Guest
        /// <summary>
        /// 根据房号查询客人信息
        /// </summary>
        /// <param name="roomNo"></param>
        /// <param name="bookDate"></param>
        /// <param name="aGuestList"></param>
        /// <returns></returns>
        public static int GetRoomNowAndFutureGuest(out List<CHotelGuest> aGuestList,string aHotelCode, string roomNo, DateTime bookDate)
        {
            int Rst = 0;
            string Url = string.Format("{0}guest/gettodayguest?today={1}&code={2}",iRequestUrl, LmmCommonFunction.DateTimeToUTC(bookDate),aHotelCode);
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aGuestList = null;
            try
            {
                CGetTodayGuestResult RespRst = JsonConvert.DeserializeObject<CGetTodayGuestResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aGuestList = new List<CHotelGuest>();
                    foreach (EmRoomGuest Guest in RespRst.iGuestList)
                    {
                        aGuestList.Add(new CHotelGuest()
                        {
                            iRoomId = Guest.iRoomId,
                            iGstNo = Guest.iGuestNo,
                            iGuestName = Guest.iGuestName,
                            iCheckFlag = Guest.iCheckFlag,
                            isCheckIn = Guest.iCheckFlag == CHotelCheckFlag.KCheckIn,
                            iSex = Guest.iSex,
                            iVip = Guest.iVip,
                            iCheckInDate = LmmCommonFunction.UTCToDateTime(Guest.iCheckInTime),
                            iCheckOutDate = LmmCommonFunction.UTCToDateTime(Guest.iCheckOutTime)
                        });
                    }
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int GetAllGuestList(out List<EmGuestLite> aGuestList, string aCode)
        {
            int Rst = 0;
            string Url = iRequestUrl + "guest/getallguestlist?code=" + aCode;
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aGuestList = null;
            try
            {
                CGetGuestListResult RespRst = JsonConvert.DeserializeObject<CGetGuestListResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aGuestList = RespRst.iGuest;
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int GetAllTeamName(out List<string> aNames,string aCode)
        {
            int Rst = 0;
            string Url = iRequestUrl + "guest/getallteams?code=" + aCode;
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aNames = null;
            try
            {
                CGetTeamNameResult RespRst = JsonConvert.DeserializeObject<CGetTeamNameResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aNames = RespRst.iTeam;
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int GetNewGuestNameByTeam(out string aGstName,string aCode,string aTeamName,int aIndex)
        {
            aGstName = aTeamName;
            int Rst = 0;
            string Url = string.Format("{0}guest/getnewgstnamebyteam?code={1}&team={2}&index={3}"
                ,iRequestUrl
                ,aCode
                ,aTeamName
                , aIndex);
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            try
            {
                CGetNeewGstNameResult RespRst = JsonConvert.DeserializeObject<CGetNeewGstNameResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aGstName = RespRst.iName;
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int GetGuestInfoById(out CHotelGuest aGuest, string aGstNo)
        {
            int Rst = 0;
            string Url = iRequestUrl + "guest/getguestinfobyid?uid=" + aGstNo;
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aGuest = new CHotelGuest();
            try
            {
                CGetGuestInfoResult RespRst = JsonConvert.DeserializeObject<CGetGuestInfoResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aGuest.iGstNo = RespRst.iGuest.iGuestNo;
                    aGuest.iGuestName = RespRst.iGuest.iGuestName;
                    aGuest.iSex = RespRst.iGuest.iSex;
                    aGuest.iVip = RespRst.iGuest.iVip;
                    aGuest.iCity = RespRst.iGuest.iCity;
                    aGuest.iCardNo = RespRst.iGuest.iCardNo;
                    aGuest.iEmailAddr = RespRst.iGuest.iEmail;
                    aGuest.iLanguage = RespRst.iGuest.iLang;
                    aGuest.iMobile = RespRst.iGuest.iMobile;
                    aGuest.iCountry = RespRst.iGuest.iNation;
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int GetBookingGuestsByRoomId(out List<CHotelGuest> aGuestList,string aCode,Int64 aRoomId,long aBookDate)
        {
            int Rst = 0;
            string Url =string.Format("{0}guest/getroombooking?code={1}&roomid={2}&thistime={3}", iRequestUrl,aCode,aRoomId,aBookDate);
            string Result = CHttpUtility.SendHttpGetRequest(Url);
            aGuestList = new List<CHotelGuest>();
            try
            {
                CGetTodayGuestResult RespRst = JsonConvert.DeserializeObject<CGetTodayGuestResult>(Result);
                Rst = RespRst.iResult;
                if (Rst >= 0)
                {
                    aGuestList = new List<CHotelGuest>();
                    foreach (EmRoomGuest Guest in RespRst.iGuestList)
                    {
                        aGuestList.Add(new CHotelGuest()
                        {
                            iRoomId = Guest.iRoomId,
                            iGstNo = Guest.iGuestNo,
                            iGuestName = Guest.iGuestName,
                            iCheckFlag = Guest.iCheckFlag,
                            isCheckIn = Guest.iCheckFlag == CHotelCheckFlag.KCheckIn,
                            iSex = Guest.iSex,
                            iVip = Guest.iVip,
                            iCheckInDate = LmmCommonFunction.UTCToDateTime(Guest.iCheckInTime),
                            iCheckOutDate = LmmCommonFunction.UTCToDateTime(Guest.iCheckOutTime),
                            iCompany = Guest.iCompany,
                            iTeamName=Guest.iTeamName
                        });
                    }
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int CheckInAndOutGuest(Int64 aRoomId, bool aIsCheckIn)
        {
            int Rst = 0;
            string Url = iRequestUrl;
            if (aIsCheckIn)
            { Url += "guest/checkin"; }
            else { Url += "guest/checkout"; }
            JObject Param = new JObject();
            Param["roomid"] = aRoomId;
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNodeJsResult RespRst = JsonConvert.DeserializeObject<CNodeJsResult>(Result);
                Rst = RespRst.iResult;
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int GuestDelay(Int64 aRoomId, DateTime aLeaveDate)
        {
            int Rst = 0;
            string Url = iRequestUrl + "guest/overstay";

            JObject Param = new JObject();
            Param["roomid"] = aRoomId;
            Param["staytime"] = LmmCommonFunction.DateTimeToUTC(aLeaveDate);
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNodeJsResult RespRst = JsonConvert.DeserializeObject<CNodeJsResult>(Result);
                Rst = RespRst.iResult;
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int CancelBooking(Int64 aRoomId, List<KeyValuePair<string,Int64>> aBookingGuests)
        {
            int Rst = 0;
            string Url = iRequestUrl + "guest/cancelbooking";

            JObject Param = new JObject();
            Param["roomid"] = aRoomId;
            JArray KvArr = new JArray();
            JObject KvObj;
            foreach (KeyValuePair<string, Int64> Guest in aBookingGuests)
            {
                KvObj = new JObject();
                KvObj["k"] = Guest.Key;
                KvObj["v"] = Guest.Value;
                KvArr.Add(KvObj);
            }
            Param["guestStr"] = KvArr.ToString(Formatting.None);
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNodeJsResult RespRst = JsonConvert.DeserializeObject<CNodeJsResult>(Result);
                Rst = RespRst.iResult;
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int NewGuestInfo(out string aGuestNo,string aCode,EmGuestInfo aGuest)
        {
            int Rst = 0;
            string Url = iRequestUrl + "guest/newguestinfo";
            aGuestNo = string.Empty;
            JObject Param = new JObject();
            Param["code"] = aCode;
            JObject KvObj= new JObject();
            KvObj["iGuestNo"] = aGuest.iGuestNo;
            KvObj["iSex"] = aGuest.iSex;
            KvObj["iGuestName"] = aGuest.iGuestName;
            KvObj["iCardNo"] = aGuest.iCardNo;
            KvObj["iVip"] = aGuest.iVip;
            KvObj["iEmail"] = aGuest.iEmail;
            KvObj["iMobile"] = aGuest.iMobile;
            KvObj["iNation"] = aGuest.iNation;
            KvObj["iCity"] = aGuest.iCity;
            KvObj["iLang"] = aGuest.iLang;
            Param["guestStr"] = KvObj.ToString(Formatting.None);
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNewItemResult RespRst = JsonConvert.DeserializeObject<CNewItemResult>(Result);
                Rst = RespRst.iResult;
                if(Rst>=0)
                {
                    aGuestNo = RespRst.iNewId.ToString("D");
                }
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }

        public static int NewComingGuest(string aCode, List<EmRoomGuest> aGuestList)
        {
            int Rst = 0;
            string Url = iRequestUrl + "guest/newcomeinguest";
            JObject Param = new JObject();
            Param["code"] = aCode;
            JArray GuestArr = new JArray();
            foreach (EmRoomGuest Guest in aGuestList)
            {
                JObject KvObj = new JObject();
                KvObj["iGuestNo"] = Guest.iGuestNo;
                KvObj["iGuestName"] = Guest.iGuestName;
                KvObj["iCheckInTime"] = Guest.iCheckInTime;
                KvObj["iCheckOutTime"] = Guest.iCheckOutTime;
                KvObj["iRoomId"] = Guest.iRoomId;
                KvObj["iRoomName"] = Guest.iRoomName;
                KvObj["iCompany"] = Guest.iCompany;
                KvObj["iTeamName"] = Guest.iTeamName;
                GuestArr.Add(KvObj);
            }
            Param["guestStr"] = GuestArr;
            string Result = CHttpUtility.SendHttpPostRequest(Url, CHttpUtility.KJsonContent, Param.ToString(Formatting.None));
            try
            {
                CNodeJsResult RespRst = JsonConvert.DeserializeObject<CNodeJsResult>(Result);
                Rst = RespRst.iResult;
            }
            catch (Exception Ex)
            {
                CCommonFunction.GetLogger().Logout(Ex.ToString());
                Rst = -1;
            }
            return Rst;
        }



        #endregion
    }


}
